package com.woniu.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.user.commons.Result;
import com.woniu.user.controller.form.LessonAddForm;
import com.woniu.user.model.Lesson;
import com.woniu.user.service.LessonService;
import com.woniu.user.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.index.ReaderSlice;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 课程信息表 前端控制器
 * </p>
 *
 * @author Silas
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/lesson")
@Slf4j
public class LessonController {
    @Resource
    private LessonService lessonService;

    @GetMapping("/my")
    public Result findMyLesson(Integer coachId) {
        log.info("查询教练{}的课程", coachId);
        List<Lesson> myLesson = lessonService.findMyLesson(coachId);
        return Result.success(200, "查询成功", myLesson);
    }

    @PostMapping("/create")
    public Result setLesson(@RequestBody LessonAddForm lessonAddForm, HttpServletRequest req) {
        Integer userId = JwtUtil.getUserIdFromReq(req);// 获取用户账号
        log.info("添加课程{}", lessonAddForm);
        lessonService.add(lessonAddForm, userId);
        return Result.success(200, "添加成功", null);
    }

    @PostMapping("/del")
    public Result del(Integer id) {
        log.info("禁用课程{}", id);
        Lesson byId = lessonService.getById(id);
        byId.setState("下架");
        lessonService.updateById(byId);
        return Result.success(200, "禁用成功", null);
    }
    @GetMapping("/query")
    public Result getMy(HttpServletRequest req){
        Integer userId = JwtUtil.getUserIdFromReq(req);// 获取用户账号
        log.info("查看{}的课程",userId);
        List<Lesson> my = lessonService.getMy(userId);
        return Result.success(200,"查询成功",my);
    }
}

